<?php

include "../../lib/start.php";

ini_set("display_errors", "1");

check_session();
check_permission(DOC_PERM);

if(!isset($_SESSION['__current_year__'])){
	$sel_anno = "SELECT * FROM anni WHERE id_anno = (SELECT MAX(id_anno) FROM anni)";
	$res_anno = $db->execute($sel_anno);
	$current_year = new AnnoScolastico($res_anno->fetch_assoc());
	$_SESSION['__current_year__'] = $current_year;
}

$free_day = false;
$label = "Oggi";
$today = date("Y-m-d");
if(date("H") > "14"){
	$today = date("Y-m-d", strtotime(date("Y-m-d")." +1 days"));
	$label = "Domani";
}
//print $today;
if(date("w", strtotime($today)) == 0){
	$free_day = true;
	$label .= " non c'&egrave; lezione. Buona domenica";
}
else{
	/* costruzione del programma del giorno:
	 * step #1: orario generale
	*/
	setlocale(LC_TIME, "it_IT");
	$str_date = strftime("%A", strtotime($today));
	$schedule = array();
	//for($i = 0; $i < 6; $i++)
	//	$schedule[$i] = array();
	$sel_sched = "SELECT orario.descrizione, orario.ora, classi.anno_corso AS cl, classi.sezione AS sez, materie.materia AS materia FROM orario, classi, materie WHERE docente = ".$_SESSION['__user__']->getUid()." AND orario.classe = classi.id_classe AND orario.materia = materie.id_materia AND giorno = '".ucfirst(substr($str_date, 0, 3))."' AND anno = ".$_SESSION['__current_year__']->get_ID()." ORDER BY ora ";
	$res_sched = $db->execute($sel_sched);
	if($res_sched->num_rows == 0){
		$free_day = true;
		$label .= " &egrave; il tuo giorno libero";
	}
	else{
		while($sched = $res_sched->fetch_assoc()){
			$schedule[$sched['ora']] = $sched;
			$schedule[$sched['ora']]['att'] = "";
			$schedule[$sched['ora']]['hw'] = "";
		}
		/* step #2: ricerca di attivita' e compiti */
		$sel_act = "SELECT * FROM impegni WHERE data_inizio LIKE '".$today."%' AND docente = ".$_SESSION['__user__']->getUid();
		$res_act = $db->execute($sel_act);
		$index = 0;
		if($res_act->num_rows > 0){
			while($act = $res_act->fetch_assoc()){
				list($d, $h) = split(" ", $act['data_inizio']);
				switch(substr($h, 0, 2)){
					case "08":
						$index = 1;
						break;
					case "09":
						$index = 2;
						break;
					case "10":
						$index = 3;
						break;
					case "11":
						$index = 4;
						break;
					case "12":
						$index = 5;
						break;
				}
				if($act['tipo'] == 1)
				$schedule[$index]['att'] = $act['descrizione'];
			}
		}
	}
}

// controllo circolari non lette o pubblicate dopo l'ultimo accesso
$sel_post = "SELECT * FROM circolari WHERE data_inserimento > (SELECT previous_access FROM utenti WHERE uid = ".$_SESSION['__user__']->getUid().")";
$res_post = $db->execute($sel_post);
$post_circ = $res_post->num_rows;
if($post_circ == 0){
	$_SESSION['__circular_msg__'] = "Nessuna circolare pubblicata dal tuo ultimo accesso";
}
else{
	$_SESSION['__circular_msg__'] = "Dal tuo ultimo accesso sono state pubblicate $post_circ circolari";
}

$sel_unread = "SELECT COUNT(*) AS count FROM circolari WHERE anno = ".$_SESSION['__current_year__']->get_ID();
$res_unread = $db->execute($sel_unread);
$_unr = $res_unread->fetch_assoc();
$unread_count = $_unr['count'];

$sel_read = "SELECT COUNT(lettura_circolari.id_circolare) AS count FROM lettura_circolari, circolari WHERE lettura_circolari.id_circolare = circolari.id_circolare AND docente = ".$_SESSION['__user__']->getUid()." AND anno = ".$_SESSION['__current_year__']->get_ID()." AND letta = 1";
$res_read = $db->execute($sel_read);
$_rd = $res_read->fetch_assoc();
$unread_count -= $_rd['count'];
$_SESSION['__unread_count__'] = $unread_count;

$sel_ev_docs = "SELECT COUNT(id) FROM documents WHERE evidenziato IS NOT NULL AND evidenziato > NOW()";
$res_ev = $db->executeCount($sel_ev_docs);

include "index.html.php";

?>